package com.example.xiaoletest;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.example.test.picedit.view.DrawView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@SuppressLint("NewApi")
public class PicEditActivity extends AppCompatActivity {

    private int type = DrawView.DRAW_TYPE_NORMAL;
    private boolean isRubber=false;
//    @BindView(R.id.picedit_draw)
//    DrawTextureView mView;
    @BindView(R.id.picedit_pic) DrawView mView;
    @BindView(R.id.picedit_color_ll) LinearLayout mColorll;
    @BindView(R.id.picedit_text_ll) LinearLayout mTextll;
    @BindView(R.id.picedit_text_et) EditText mTextEt;
    @BindView(R.id.test_iv) ImageView iv;

    //btn
    @BindView(R.id.picedit_red_btn)ImageButton mRedBtn;
    @BindView(R.id.picedit_blue_btn)ImageButton mBlueBtn;
    @BindView(R.id.picedit_yellow_btn)ImageButton mYellowBtn;
    @BindView(R.id.picedit_green_btn)ImageButton mGreenBtn;
    @BindView(R.id.picedit_orange_btn)ImageButton mOrangeBtn;
    @BindView(R.id.picedit_pen_btn)Button mPenBtn;
    @BindView(R.id.picedit_color_btn)Button mColorBtn;
    @BindView(R.id.picedit_text_btn)Button mTextBtn;
    @BindView(R.id.picedit_rubber_btn)Button mRubberBtn;
    @BindView(R.id.picedit_del_btn)Button mDelBtn;
    @BindView(R.id.picedit_last_btn)Button mLastBtn;
    @BindView(R.id.picedit_save_btn)Button mSaveBtn;




    Bitmap mBitmap=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picedit);
        ButterKnife.bind(this);
        initView();
        initData();
//        initTest();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mView.destory();
        if (mBitmap!=null){
            mBitmap.recycle();
            mBitmap = null;
        }
    }

    @OnClick(R.id.picedit_red_btn)
    void onClickRedBtn(){
        mView.setPaintColor(getColor(R.color.red));
        mView.setTextColor(getColor(R.color.red));
        initColorBtn();
        mRedBtn.setBackground(getDrawable(R.drawable.btn_round_red_selected));
    }

    @OnClick(R.id.picedit_blue_btn)
    void onClickBlueBtn(){
        mView.setPaintColor(getColor(R.color.blue));
        mView.setTextColor(getColor(R.color.blue));
        initColorBtn();
        mBlueBtn.setBackground(getDrawable(R.drawable.btn_round_blue_selected));
    }
    @OnClick(R.id.picedit_yellow_btn)
    void onClickYellowBtn(){
        mView.setPaintColor(getColor(R.color.yellow));
        mView.setTextColor(getColor(R.color.yellow));
        initColorBtn();
        mYellowBtn.setBackground(getDrawable(R.drawable.btn_round_yellow_selected));
    }
    @OnClick(R.id.picedit_green_btn)
    void onClickGreenBtn(){
        mView.setPaintColor(getColor(R.color.green));
        mView.setTextColor(getColor(R.color.green));
        initColorBtn();
        mGreenBtn.setBackground(getDrawable(R.drawable.btn_round_green_selected));
    }
    @OnClick(R.id.picedit_orange_btn)
    void onClickOrangeBtn(){
        mView.setPaintColor(getColor(R.color.orange));
        mView.setTextColor(getColor(R.color.orange));
        initColorBtn();
        mOrangeBtn.setBackground(getDrawable(R.drawable.btn_round_orange_selected));
    }

    @OnClick(R.id.picedit_pen_btn)
    void onClickPenBtn(){
        initFunBtn();
        if (isRubber)type = DrawView.DRAW_TYPE_NORMAL;
        if (type==DrawView.DRAW_TYPE_DRAW){
            type = DrawView.DRAW_TYPE_NORMAL;
            mView.setDrawType(type);
            mView.setEraser(false);
            isRubber = false;
        }else{
            Log.i("123","DRAW_TYPE_DRAW");
            type = DrawView.DRAW_TYPE_DRAW;
            mView.setDrawType(type);
            mView.setEraser(false);
            isRubber = false;
            mView.confirmText();
            mPenBtn.setBackground(getDrawable(R.drawable.btn_round_style2_selected));
        }
    }

    @OnClick(R.id.picedit_color_btn)
    void onClickColorBtn(){
        if(mColorll.getVisibility()==View.GONE){
            mColorll.setVisibility(View.VISIBLE);
            mColorBtn.setBackground(getDrawable(R.drawable.btn_round_style2_selected));
        }else{
            mColorll.setVisibility(View.GONE);
            mColorBtn.setBackground(getDrawable(R.drawable.btn_round_style2));
        }

    }

    @OnClick(R.id.picedit_text_btn)
    void onClickTextBtn(){
        initFunBtn();
        if (type==DrawView.DRAW_TYPE_TEXT){
            type = DrawView.DRAW_TYPE_NORMAL;

            mView.confirmText();
            mView.setDrawType(type);

        }else{
            type = DrawView.DRAW_TYPE_TEXT;
            mView.setDrawType(type);
            mTextBtn.setBackground(getDrawable(R.drawable.btn_round_style2_selected));
        }
    }

    @OnClick(R.id.picedit_rubber_btn)
    void onClickRubberBtn(){
        initFunBtn();
        if (mView.getEraser()){
            mView.setEraser(false);
            mView.setDrawType(DrawView.DRAW_TYPE_NORMAL);
            isRubber = false;
        }else{
            mView.setDrawType(DrawView.DRAW_TYPE_DRAW);
            mView.setEraser(true);
            mRubberBtn.setBackground(getDrawable(R.drawable.btn_round_style2_selected));
            mView.confirmText();
            isRubber=true;
        }

    }

    @OnClick(R.id.picedit_last_btn)
    void onClickLastBtn(){
        mView.lastStep();
    }

    @OnClick(R.id.picedit_save_btn)
    void onSaveBtn(){
        Bitmap bp = mView.saveBitmap();
        iv.setImageBitmap(bp);
        iv.setVisibility(View.VISIBLE);


    }

    @OnClick(R.id.picedit_del_btn)
    void onDelBtn(){

    }

    private void initView(){



        initColorBtn();
        mColorll.setVisibility(View.GONE);

        mTextEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mView.setText(s.toString().trim());
            }
        });
    }

    private void initColorBtn(){
        mRedBtn.setBackground(getDrawable(R.drawable.btn_round_red));
        mBlueBtn.setBackground(getDrawable(R.drawable.btn_round_blue));
        mYellowBtn.setBackground(getDrawable(R.drawable.btn_round_yellow));
        mGreenBtn.setBackground(getDrawable(R.drawable.btn_round_green));
        mOrangeBtn.setBackground(getDrawable(R.drawable.btn_round_orange));
    }

    private void initFunBtn(){
        mPenBtn.setBackground(getDrawable(R.drawable.btn_round_style2));
        mTextBtn.setBackground(getDrawable(R.drawable.btn_round_style2));
        mRubberBtn.setBackground(getDrawable(R.drawable.btn_round_style2));

    }

    private void initTest(){
        Glide.with(this).load(R.drawable.face1).into(iv);

    }
    private void initData(){
        Glide.with(this)
                .load(R.drawable.mysong)
                .asBitmap()
                .centerCrop()
                .into(new SimpleTarget<Bitmap>() {

                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        Log.i("123","on resource ready ");
//                        mView.setImageBitmap(resource);
                        Matrix matrix = new Matrix();
                        int width = resource.getWidth();
                        int height = resource.getHeight();
                        WindowManager wm = (WindowManager) PicEditActivity.this.getSystemService(Context.WINDOW_SERVICE);
                        int wmWidth = wm.getDefaultDisplay().getWidth();
                        float scal = ((float) wmWidth)/width;

                        matrix.postScale(scal,scal);
                        if (mBitmap!=null){
                            mBitmap.recycle();
                        }
                        mBitmap = Bitmap.createBitmap(resource,0,0,width,height,matrix,true);
                        Log.i("123","bmp  width="+mBitmap.getWidth()+"  h="+mBitmap.getHeight());
                        mView.setBasePicture(mBitmap);
//                        iv.setVisibility(View.VISIBLE);
//                        iv.setImageBitmap(resource);
                        resource.recycle();
                    }
                });


        mView.setEditText(mTextEt);
        mView.setEditView(mTextll);
        mView.setPaintColor(getColor(R.color.red));
        mView.setTextColor(getColor(R.color.red));
        mView.setSuperMode(true);
    }

}
